// pages/dailyclean/dailyclean.js
const httpRequest = require('../../utils/http_utils.js')
const dateutil = require('../../utils/date_util.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '日常保洁',
    menulist: [],
    PageSize: 10, //单页数据条数
    CurrentPage: 1, //当前页码
    RoomGuid: '', //房间guid
    TaskStatus: '', //任务状态
    starttime: '', //任务起始日期
    endtime: '', //任务截止日期
    TaskName: '', //任务名称
    tasklist: [], //当日未保洁任务列表
    totalNum: 0, //任务总数
    info: '', //提示信息
    ParentGuid: '', //菜单父节点guid
    downloading: false, //加载中
    noMore: false, //没有更多了
    loadingFailed: false, //数据加载失败
    showzanwu: false,
    customHeight: 0, //自定义高度
  },

  tiaozhuan(e) { //页面跳转
    var rowguid = e.currentTarget.dataset.rowguid; //获取菜单rowguid
    var url = e.currentTarget.dataset.url; //获取按钮跳转地址
    if (url.indexOf('saoyisao') == -1) {
      wx.navigateTo({
        url: url + '?ParentGuid=' + rowguid,
      })
    } else {
      this.scancode();
    }
  },

  //扫一扫
  scancode: function() {
    var that = this
    wx.scanCode({
      success: function(res) {
        console.log(res)
        if (res.result != '') {
          //判断是否有该房间
          //有房间跳转
          var param = {
            RoomCode: res.result
          }
          httpRequest.GetValidateRequest(httpRequest.apiInfo.JugeRoomExistApi, param,
            function(room) {
              console.log(room)
              if (room.flag == '0') {
                wx.navigateTo({
                  url: '/pages/cleanRecordInsert/cleanRecordInsert?RoomRowGuid=' + room.RoomRowGuid//跳转保洁详情
                })
              } else {
                that.setData({
                  info: '请扫房间二维码'
                })
              }
            },
            function(err) {
              console.error(err)
              that.setData({
                info: '请扫房间二维码'
              })
            })
        } else {
          that.setData({
            info: '请扫房间二维码'
          })
        }
      },
    })
  },

  initData: function() { //初始化数据
    var that = this;
    var param = {
      PageSize: that.data.PageSize,
      CurrentPage: that.data.CurrentPage,
      RoomGuid: that.data.RoomGuid,
      TaskStatus: that.data.TaskStatus,
      starttime: that.data.starttime,
      endtime: that.data.endtime,
      TaskName: that.data.TaskName,
    }
    var taskArray = that.data.tasklist;
    httpRequest.GetValidateRequest(httpRequest.apiInfo.GetCleanTaskListMobileApi, param,
      function(data) {
        console.log('当日未保洁任务', data)
        if (data.totalNum == 0) {
          that.setData({
            showzanwu: true
          })
        } else {
          for (var i = 0; i < data.pageData.length; i++){
            var params = {
              RowGuid: data.pageData[i].RowGuid,
              RoomName: data.pageData[i].RoomName,
              CleanName: data.pageData[i].CleanName,
              CleaningCycle: data.pageData[i].CleaningCycle,
              TaskBeginTime: dateutil.dateFormat6(data.pageData[i].TaskBeginTime),
              TaskEndedTime: dateutil.dateFormat6(data.pageData[i].TaskEndedTime),
              Tasklocation: data.pageData[i].Tasklocation,
            }
            taskArray.push(params)
          }
          that.setData({
            totalNum: data.totalNum,
            tasklist: taskArray,
            showzanwu: false
          })
        }
      },
      function(err) {
        console.error(err)
        that.setData({
          showzanwu: true
        })
      })
  },

  //获取本页菜单
  getMenuList() {
    var menu_list = this.data.menulist;
    let ParentGuid = this.data.ParentGuid
    var storage_list = wx.getStorageSync('UserInfo').AppLogin.AppModuleDTO.filter(function(item) {
      return item.ParentGuid === ParentGuid;
    });

    for (var i = 0; i < storage_list.length; i++) {
      var param = {
        RowGuid: storage_list[i].RowGuid,
        Name: storage_list[i].ModuleName,
        Icon: storage_list[i].BigIconAddress,
        ModuleUrl: storage_list[i].ModuleUrl
      }
      menu_list.push(param)
    }
    //根据RowGuid去重
    const RowGuid = 'RowGuid';
    menu_list = menu_list.reduce((all, next) => all.some((atom) => atom[RowGuid] == next[RowGuid]) ? all : [...all, next], []);
    this.setData({
      menulist: menu_list
    })
    console.log('menulist---', this.data.menulist)
  },

  //上拉加载
  scrollToLower: function() {
    var that = this
    console.log(that.data.totalNum + "_______总条数")
    var pages = Math.round((that.data.totalNum / that.data.PageSize) * 100) / 100
    if (that.data.CurrentPage > pages) {
      that.setData({
        noMore: true
      })
      setTimeout(() => {
        that.setData({
          noMore: false
        })
      }, 2000)
    } else {
      that.setData({
        noMore: false,
        downloading: true
      })
      setTimeout(() => {
        that.setData({
          downloading: false
        })
      }, 3000)
      that.setData({
        CurrentPage: that.data.CurrentPage + 1
      })
      that.initData();
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    this.setData({
      customHeight: ((app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 200
    })
    console.log('---------导航栏高度--------' + this.data.customHeight)

    if (options.ParentGuid != undefined && options.ParentGuid != '') {
      this.setData({
        ParentGuid: options.ParentGuid
      })
    }
    console.log('日常保洁ParentGuid', this.data.ParentGuid)
    this.getMenuList();
    // this.initData();
    this.refreshView = this.selectComponent("#refreshView");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log('onShow日常保洁ParentGuid', this.data.ParentGuid)
    this.setData({
      tasklist: []
    })
    this.initData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    setTimeout(() => {
      console.log("scrollToUpper")
      var that = this
      that.setData({
        PageSize: that.data.PageSize,
        CurrentPage: 1,
        RoomGuid: that.data.RoomGuid,
        TaskStatus: that.data.TaskStatus,
        starttime: that.data.starttime,
        endtime: that.data.endtime,
        TaskName: that.data.TaskName,
        tasklist: []
      })
      that.initData();
      this.refreshView.stopPullRefresh()
    }, 1000)
  },

  //触摸开始
  handletouchstart: function(event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function(event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function(event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function(event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function(event) {
    this.refreshView.onPageScroll(event)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})